package com.zjsy.interceptor;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 登录拦截器
 * @author lenovo
 *
 */
public class LoginInterceptor implements HandlerInterceptor{

	//在Controller方法执行后被执行
	//处理异常和日志
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
		
	}

	//在Controller方法被执行后，返回一个ModelAndView前被执行
	//设置或者清理页面共用参数等等
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		
	}

	//在Controller方法执行前被执行
	//登录拦截、权限认证
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		System.out.println(request.getRequestURL());
		System.out.println("=====");
		if(request.getSession().getAttribute("user")==null) {
			response.sendRedirect(request.getContextPath() + "/login");
			return false;
		}
		return true;
	}

}
